<template>
  <div class='container'>
    <el-dialog @open="openadd" title="title" :visible="isShowDept" :before-close="handleClose">
      <el-form ref="addRef" :model="form" :rules="rules">
        <el-form-item label="学科名称" :label-width="formLabelWidth" prop="subjectName">
          <el-input v-model="form.subjectName" autocomplete="off"></el-input>
        </el-form-item>
        <el-switch style="display: block" v-model="form.isFrontDisplay" active-color="#13ce66" inactive-color="#ff4949" active-text="按月付费">
        </el-switch>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleSubmit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects.js'
export default {
  name: 'SubjectsAdd',
  props: {
    isShowDept: {
      type: Boolean,
      required: true
    },
    operateType: {
      type: String,
      default: 'add'
    },
    formda: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      form: {
        subjectName: '',
        isFrontDisplay: true
      },
      value1: true,
      value2: true,
      formLabelWidth: '10%',
      rules: {
        subjectName: [{ required: true, message: '学科名称不能为空', trigger: 'blur' }]
      }
    }
  },
  // computed:{
  //   title(){
  //     return this.
  //   }
  // },
  methods: {
    openadd () {
      if (this.formda.id) {
        this.form = this.formda
      } else {
        this.form = this.$options.data().form
      }
    },
    // 提交表单
    async handleSubmit () {
      if (this.form.id) {
        const ret2 = await update(this.form)
        this.form = {
          subjectName: '',
          isFrontDisplay: true
        }
        console.log(ret2)
      } else {
        const ret1 = await add(this.form)
        console.log(ret1)
      }
      // 关闭弹窗
      this.$emit('update:isShowDept', false)
      this.$parent.loadList()
    },
    handleClose () {
      this.form.id = ''
      // 关闭弹窗
      this.$emit('update:isShowDept', false)
    }
  }
}
</script>

<style scoped lang='less'>
</style>
